Micron Document
Deavmi's coding shack

Node / mirrors / go-niknaks / commits / a9f6f0d

Commit a9f6f0dcf4cc00a9c40d80d6115c77ad178fd8bd


Parents : e16b85a
Author : Tristan Brice Velloza Kildaire <deavmi@redxen.eu>
Date : 2026-04-08T16:48:46+02:00

Updated bucket code

Changes

2 files changed, 74 insertions(+), 0 deletions(-)


Diff

diff --git a/bucket/bucket.go b/bucket/bucket.go
new file mode 100644
index 0000000..a45e6bb
--- /dev/null
+++ b/bucket/bucket.go
@@ -0,0 +1,55 @@
+package bucket
+
+import "fmt"
+
+// a keyable data structure that
+// let's you stack items into a
+// list indexed by a certain key
+//
+// key1 -> [A, B, C]
+// key2 -> [A, B, C]
+type Bucket[Key comparable, Value any] struct {
+ _map map[Key][]Value
+}
+
+func New[Key comparable, Value any]() *Bucket[Key, Value] {
+ return &Bucket[Key, Value]{_map: map[Key][]Value{}}
+}
+
+func (b *Bucket[Key, Value]) Values(key Key) ([]Value, error) {
+ if !b.HasKey(key) {
+ return []Value{}, fmt.Errorf("Could not find key '%v'", key)
+ } else {
+ // TODO: Maybe make a copy of the array so that user
+ // can't modify it?
+ return b._map[key], nil
+ }
+}
+
+func (b *Bucket[Key, Value]) HasKey(key Key) bool {
+ _, exists := b._map[key]
+ return exists
+}
+
+func (b *Bucket[Key, Value]) Place(key Key, value Value) {
+ // _, _ := b._map[key]
+
+ b._map[key] = append(b._map[key], value)
+}
+
+func (b *Bucket[Key, Value]) Keys() []Key {
+ var keys []Key
+ for k, _ := range b._map {
+ keys = append(keys, k)
+ }
+ return keys
+}
+
+func (b *Bucket[Key, Value]) RemoveKey(key Key) error {
+ if !b.HasKey(key) {
+ return fmt.Errorf("Could not find key '%v'", key)
+ } else {
+ delete(b._map, key)
+ return nil
+ }
+}

diff --git a/bucket/bucket_test.go b/bucket/bucket_test.go
new file mode 100644
index 0000000..be6b6e9
--- /dev/null
+++ b/bucket/bucket_test.go
@@ -0,0 +1,19 @@
+package bucket
+
+import (
+ "testing"
+
+ "new.git.deavmi.assigned.network/deavmi/go-assertions.git"
+)
+
+func Test_basic(t *testing.T) {
+ b := New[string, string]()
+ b.Place("key1", "A")
+ b.Place("key1", "B")
+
+ assertions.AssertTrueT(t, b.HasKey("key1"))
+
+ vals_out1, e1 := b.Values("key1")
+ assertions.AssertNullT(t, e1)
+ assertions.AssertEqualsT(t, vals_out1, []string{"A", "B"})
+}

Served by rngit 1.5.0 - Generated in 0.05s